package com.gitee.zhangchenyan.takin.common.utils;

import java.time.LocalDateTime;


import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSON;
import com.gitee.zhangchenyan.takin.common.result.ExceptionLog;
import com.gitee.zhangchenyan.takin.service.IExceptionLogService;
import com.mysql.cj.jdbc.MysqlDataSource;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.springframework.boot.logging.LogLevel;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Arrays;
import java.util.logging.Logger;

@Slf4j
public class ExceptionUtils {


    /**
     * @param ex 异常
     */
    public static String outputStr(Throwable ex) {
        return outputStr(ex, "");
    }


    /**
     * @param ex        异常
     * @param errorName 异常名称
     * @return 异常结果
     */
    public static String outputStr(Throwable ex, String errorName) {
        return outputStr(ex, errorName, LogLevel.ERROR);
    }

    /**
     * @param ex        异常
     * @param errorName 异常名称
     * @param logLevel  日志级别 支持error和warn
     * @return 异常结果
     */
    public static String outputStr(Throwable ex, String errorName, LogLevel logLevel) {
        try {
            String message = StrUtil.isNotEmpty(ex.getMessage()) ? ex.getMessage() : "";
            String toString = StrUtil.isNotEmpty(ex.toString()) ? ex.toString() : "";
            String stackTrace = ex.getStackTrace() != null && ex.getStackTrace().length > 0 ? ex.getStackTrace()[0].toString() : "";
            if (logLevel == LogLevel.ERROR) {
                log.error(errorName + "异常getMessage:" + message);
                log.error(errorName + "异常toString:" + toString);
                log.error(errorName + "异常stackTrace:" + stackTrace);
            }
            if (logLevel == LogLevel.WARN) {
                log.warn(errorName + "异常getMessage:" + message);
                log.warn(errorName + "异常toString:" + toString);
                log.warn(errorName + "异常stackTrace:" + stackTrace);
            }
            String shortGetMessage = message.length() > 300 ? message.substring(300) : message;
            String shortExToString = toString.length() > 300 ? toString.substring(300) : toString;
            String shortStackTrace = stackTrace.length() > 300 ? stackTrace.substring(300) : stackTrace;
            try {
                ExceptionLog exceptionLog = new ExceptionLog();
                exceptionLog.setLevel(logLevel.toString());
                exceptionLog.setName(errorName);
                exceptionLog.setToString(toString);
                exceptionLog.setMessage(message);
                exceptionLog.setStackTrace(JSON.toJSONString(ex.getStackTrace()));
                exceptionLog.setCreationTime(LocalDateTimeUtil.now());
                IExceptionLogService exceptionLogService = SpringUtil.getBean(IExceptionLogService.class);
                exceptionLogService.save(exceptionLog);
            } catch (Exception e) {
                log.warn("异常记录service失败:" + e.toString());
            }
            return shortGetMessage + "||" + shortExToString + "||" + shortStackTrace;
        } catch (Exception e) {
            log.error("异常处理过程异常:" + e.toString());
            return "";
        }
    }
}
